<?php
namespace Game\Controller;
use Common\Controller\CommonController;
class UserController extends CommonController{

    const AppID     =   'wx0dcb83c5892f258b';
    const AppSecret =   'aab64931415e6bb57986cb832518fc98';

    //用户注册或获取uid，用户登陆(更新数据)
    public function login()
    {
        $app_data               =   array('wx0dcb83c5892f258b'=>'aab64931415e6bb57986cb832518fc98');
        $data                   =   array();
        $user_data              =   array();
        $code                   =  	I('post.code','','htmlspecialchars');
        $appid                  =  	I('post.appid','','htmlspecialchars');
        $info 					= 	$_POST['info'];
        $info 					= 	json_decode($info,true);

        $table                  =   'game_user';

        if (! empty($code) && ! empty($appid))
        {
            $AppSecret              =   $app_data[$appid];
            $url = "https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$AppSecret."&js_code=$code&grant_type=authorization_code";

            $return_data = $this->curlget($url);

            if (empty($return_data['openid']))
            {
                $this->returnData($data,$msg='openid获取失败',$code=400);
            }

            $uid            =   0;
            $where          =   array();
            $where['openid']=   $return_data['openid'];
            $where['appid'] =   $appid;
            $field          =   array('uid','nickname','avatarurl','openid','login_num');
            $result         =   M($table)->where($where)->field($field)->find();

            if ( ! empty($info))
            {
                $user_data['nickname']      =   $info['nickName'];
                $user_data['avatarurl']     =   $info['avatarUrl'];
            }

            if (empty($result))
            {
                $user_data['appid']       =   $appid;
                $user_data['openid']      =   $return_data['openid'];
                $user_data['create_time'] =   time();
                $user_data['login_time']  =   time();
                $user_data['session_key'] =   $return_data['session_key'];
                $uid                      =   M($table)->add($user_data);
            }
            else
            {
                $uid                        =   $result['uid'];
                $user_data['login_time']    =   time();
                $user_data['session_key']   =   $return_data['session_key'];
                M($table)->where($where)->save($user_data); // 根据条件更新记录
            }

            $data['openid']     =   $return_data['openid'];
            $data['session_id'] =   $this->getSessionId();
            $data['uid']        =   $uid;
            $data['user_info']  =   $user_data;
            $this->returnData($data,'ok',200);
        }
        else
        {
            $this->returnData($data,$msg='code或game_id不能为空',$code=400);
        }
    }

    public function info()
    {
        $uid    =  	I('post.uid','','htmlspecialchars');
        if (! empty($uid))
        {
            $where          =   array();
            $where['uid']   =   $uid;
            $field          =   array('uid','nickname','avatarurl','openid');
            $result         =   M('game_user')->where($where)->field($field)->find();

            $data   =   array();
            if (! empty($result))
            {
                $img_src    =   '';
                if (! empty($result['avatarurl']))
                {
                    $img_file   = $result['avatarurl'];
                    $img_info   = getimagesize($img_file);
                    $str        = file_get_contents($result['avatarurl']);
                    $img_src    = 'data:' . $img_info['mime'] . ';base64,' . base64_encode($str);
                }

                $data   =   $result;
                $data['avatarurl']  =   $img_src;
                $this->returnData($data,'ok',200);
            }
            else
            {
                $this->returnData($data,$msg='非法访问',$code=400);
            }
        }
    }

    /**
     * 图片转base64
     * @param ImageFile String 图片路径
     * @return 转为base64的图片
     */
    function Base64EncodeImage($ImageFile) {

        $file = $ImageFile;
        if($fp = fopen($file,"rb", 0))
        {
            $gambar = fread($fp,filesize($file));
            fclose($fp);


            $base64 = chunk_split(base64_encode($gambar));
            // 输出
            return 'data:image/jpg/png/gif;base64,' . $base64 ;
        }
    }
}